package com.ruoyi.project.hit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.hit.domain.SystemDict;
import com.ruoyi.project.hit.service.CardUserService;
import com.ruoyi.project.hit.service.SystemDictService;
import com.ruoyi.project.hit.mapper.SystemDictMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author yifeng868
* @description 针对表【system_dict】的数据库操作Service实现
* @createDate 2024-05-31 14:08:19
*/
@Service
public class SystemDictServiceImpl extends ServiceImpl<SystemDictMapper, SystemDict>
    implements SystemDictService{
    @Resource
    private SystemDictMapper systemDictMapper;
    @Resource
    private CardUserService cardUserService;

    @Override
    public List<SystemDict> selectDictList(SystemDict dict) {
        LambdaQueryWrapper<SystemDict> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SystemDict::getDictType,dict.getDictType());
        if (StringUtils.isNotEmpty(dict.getDictValue())){
            wrapper.like(SystemDict::getDictValue,dict.getDictValue());
        }
        return systemDictMapper.selectList(wrapper);
    }

    @Override
    public int deleteDictById(Integer id) {
        //先判断是否被占用
        SystemDict systemDict = systemDictMapper.selectById(id);
        //调用用户服务查询dict_key是否被占用
        Long count=cardUserService.selectByDictKey(systemDict.getDictKey());
        if (count>0){
            throw new CustomException("error");
        }
        //调用中奖配置服务查询dict_key是否被占用
        return systemDictMapper.deleteById(id);
    }
}




